package ru.bragnikita.utils.logexplorer.web;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import java.io.File;

/**
 * @author Nikita.Brazhnikov
 */
@FacesConverter("FileSizeConverter")
public class FileSizeConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
		return null;
	}

	@Override
	public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) {
		if (o == null)
			return "";
		if (o instanceof Long) {
			Long value = (Long) o;
			return ConvertationUtils.fileSizeFromBytesToString(value);
		}
		if (o instanceof File) {
			File f = (File) o;
			if (f.isDirectory()) {
				return "-";
			} else return ConvertationUtils.fileSizeFromBytesToString(f.length());
		}
		return "";
	}
}
